#include "svgview.h"

SvgView::SvgView(QWidget* parent) :
    QGraphicsView(parent),CurrentAction(NOACTION), isDragOn(false),CurrentItem(nullptr)
{
    // 设置画笔的默认颜色为黑色
    PenForLine.setColor("black");
    // 设置画笔的默认宽度为1个像素
    PenForLine.setWidth(1);
    BrushForShape = Qt::NoBrush;
    // 设置渲染提示，开启抗锯齿功能，使绘制的图形边缘更加平滑
    setRenderHint(QPainter::Antialiasing);
    // 设置视图的拖拽模式为滚动手势拖拽模式
    setDragMode(ScrollHandDrag);
    // 创建一个定时器对象
    timer = new QTimer;
    // 每当定时器超时时，将触发 TimeOutSlots 槽函数的执行
    connect(timer, &QTimer::timeout, this, &SvgView::TimeOutSlots);
}

void SvgView::keyPressEvent(QKeyEvent* event){
    if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
        QGraphicsScene* scene = this->scene();
        // 获取当前选中的图元项
        QList<QGraphicsItem*> selectedItems = scene->selectedItems();
        // 遍历选中的图元项并删除
        foreach (QGraphicsItem* item, selectedItems) {
            CShape* shape = qgraphicsitem_cast<CShape*>(item);
            if (shape) {
                scene->removeItem(shape);
                delete shape;
            }
        }
        // 清空选中的图元项
        scene->clearSelection();
    }
    else QGraphicsView::keyPressEvent(event);
}
void SvgView:: wheelEvent(QWheelEvent* event){
    // 获取滚轮滚动的角度
    int delta = event->angleDelta().y();

    // 缩放因子，根据滚动角度确定放大或缩小
    qreal scaleFactor = (delta > 0) ? 1.1 : 0.9;

    // 获取当前视图中心点的坐标

    // 进行缩放操作
    scale(scaleFactor, scaleFactor);

    // 阻止事件传递给父类处理
    event->accept();
}

void SvgView::mousePressEvent(QMouseEvent* event) {
    //  清除场景中所有项的选择状态，取消选中状态
    scene()->clearSelection();
    if (CurrentAction == NOACTION) {
        if(isDragOn) setCursor(Qt::CursorShape::ClosedHandCursor);
        QGraphicsView::mousePressEvent(event);
    }
    else if(event->button() & Qt::LeftButton) {
        endPoint = startPoint = mapToScene(event->pos());
        switch (CurrentAction) {
        case LINE:
            CurrentItem = new SvgLineItem(startPoint,endPoint,PenForLine.width(),PenForLine.color(),BrushForShape);
            break;
        case RECTANGLE:
            CurrentItem = new SvgRectangleItem(startPoint,endPoint,PenForLine.width(),PenForLine.color(),BrushForShape);
            break;
        case TRIANGLE:
            CurrentItem = new SvgTriangleItem(startPoint,endPoint,PenForLine.width(),PenForLine.color(),BrushForShape);
            break;
        case ELLIPSE:
            CurrentItem = new SvgEllipseItem(startPoint,endPoint,PenForLine.width(),PenForLine.color(),BrushForShape);
            break;
        case BEZIER:
            CurrentItem = new SvgBezierItem(startPoint,endPoint,PenForLine.width(),PenForLine.color(),BrushForShape);
            break;
        default:
            break;
        }
        scene()->addItem(CurrentItem);
        timer->start(TIMEOUT);
    }
}
void SvgView::mouseMoveEvent(QMouseEvent* event) {
    if (CurrentAction == NOACTION) QGraphicsView::mouseMoveEvent(event);
    else {
        // 鼠标事件的位置转换为场景坐标系中的点，并将其作为结束点 endPoint
        endPoint = mapToScene(event->pos());
    }
}
void SvgView::mouseReleaseEvent(QMouseEvent* event) {
    timer->stop();
    if (CurrentAction == NOACTION) {
        if(isDragOn) setCursor(Qt::CursorShape::OpenHandCursor);
        QGraphicsView::mouseReleaseEvent(event);
    }
    else {
        CurrentItem->SetStartandEnd(startPoint,mapToScene(event->pos()));
        CurrentItem->GenerateControlPoints();
        CurrentItem->setSelected(true);
        // 更新视口，强制重绘视图
        viewport()->update();
    }
}
